<template>
  <div class="visual">
    <div class="header">
      <div class="left">
        <div class="title">数据可视化</div>
      </div>
    </div>
    <div class="container">
      <div class="left">
        <div class="list">
          <el-select
            v-model="taskId"
            clearable
            placeholder="请选择"
            @change="selectTask"
          >
            <el-option
              v-for="item in taskList"
              :key="item.id"
              :label="item.taskName"
              :value="item.taskId"
            >
            </el-option>
          </el-select>
          <div class="user">
            <div
              v-for="user in testUserList"
              :key="user.id"
              @click="selectId(user.id)"
              :class="ids.includes(user.id) ? 'active' : ''"
            >
              {{ user.name }}
            </div>
          </div>
          <div class="params">
            <el-checkbox-group v-model="checkParams">
              <el-checkbox
                v-for="(item, index) in paramOptions"
                :label="item.title"
                :key="index"
                >{{ item.title }}</el-checkbox
              >
            </el-checkbox-group>
          </div>
        </div>
        <el-button type="primary" @click="search">查看对比</el-button>
      </div>
      <div class="right">
        <div class="item" v-for="(item, index) in params" :key="index">
          <div class="title">{{ item.title }}</div>
          <div class="chart">
            <line-chart :optionData="item.optionData"></line-chart>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getTask,
  getTaskDetail,
  getLineData,
  getLineData1,
} from "@/api/management";

export default {
  name: "Visual",
  data() {
    return {
      ids: [],
      taskList: [],
      testUserList: [],
      paramOptions: [],
      checkParams: [],
      taskId: null,
      params: [],
    };
  },
  created() {
    this.getTaskData();
  },
  methods: {
    getTaskData() {
      getTask().then((res) => {
        this.taskList = res.data;
      });
    },
    selectTask(item) {
      if (item) {
        getTaskDetail({ taskId: item }).then((res) => {
          this.paramOptions = res.data.flyFieldList;
          this.testUserList = res.data.flyTaskList;
        });
      } else {
        this.params = [];
        this.paramOptions = [];
        this.testUserList = [];
      }
    },
    async search() {
      this.params = [];
      this.checkParams.map((item) =>
        this.params.push(
          ...this.paramOptions.filter((option) => option.title == item)
        )
      );
      if (this.ids.length == 0 || this.params.length == 0) {
        this.$modal.msgWarning("未选择对比数据");
        return;
      }
      for (let item of this.params) {
        let res;
        if (this.taskId === "20240613") {
          res = await getLineData1({
            fields: item.fields,
            ids: this.ids,
          });
        } else {
          res = await getLineData({
            fields: item.fields,
            ids: this.ids,
          });
        }
        this.$set(item, "optionData", res.data);
      }
    },
    selectId(id) {
      let index = this.ids.indexOf(id);
      if (index == -1) {
        this.ids.push(id);
      } else {
        this.ids.splice(index, 1);
      }
    },
  },
};
</script>
<style lang="scss">
.visual {
  padding: 36px 42px;

  .header {
    display: flex;
    justify-content: space-between;

    .left {
      .title {
        height: 26px;
        line-height: 26px;
        font-size: 26px;
        color: #333333;
      }
    }
  }

  .container {
    margin-top: 36px;
    display: flex;
    justify-content: space-between;
    width: 100%;

    .left {
      padding: 24px 40px 30px;
      width: 20%;
      background: #ffffff;
      box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.04);
      border-radius: 20px;

      .list {
        width: 100%;
        overflow: hidden;

        .el-select {
          width: 100%;
          .el-input {
            width: 100%;
            .el-input__inner {
              padding-left: 24px;
              width: 100%;
              height: 36px;
            }
          }
        }
        .user {
          margin-top: 20px;
          border-radius: 12px;
          padding: 16px 0;
          height: 600px;
          overflow: auto;
          border: 1px solid #dae5ff;
          & > div {
            padding: 6px 0 6px 40px;
            border-radius: 6px;
            cursor: pointer;
            &:hover {
              background: #f5f8ff;
              color: #1890ff;
            }
          }
          & > div + div {
            margin-top: 2px;
          }
          .active {
            background: #f5f8ff;
            color: #1890ff;
          }
        }
        .params {
          padding: 12px 24px;
          line-height: 30px;
          border-radius: 4px;
          .el-checkbox-group {
            display: flex;
            flex-direction: column;
          }
        }
      }

      .el-button {
        width: 100%;
        height: 48px;
        background: #365eff;
        border-radius: 12px;
        font-size: 18px;

        &:hover {
          opacity: 0.8;
        }
      }
    }

    .right {
      padding: 2% 3%;
      width: 78%;
      background: #ffffff;
      box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.04);
      border-radius: 20px;
      text-align: center;
      font-weight: 400;
      overflow: auto;
      & > div + div {
        margin-top: 24px;
      }
      .item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .title {
          width: 66px;
          font-size: 16px;
          color: #b2bec2;
        }
        .chart {
          width: 92%;
          height: 200px;
          overflow: auto;

          &::-webkit-scrollbar {
            display: block !important;
            height: 4px;
          }

          &::-webkit-scrollbar-thumb {
            background-color: #1890ff;
            border-radius: 2px;
          }
        }
      }
    }
  }
}
</style>
